package com.nxw.springcloud.gateway;

import com.alibaba.fastjson.JSONObject;
import com.chanjet.common.endpoint.pojo.ChanjetValueResult;
import com.nxw.springcloud.gateway.config.RateLimitConfig;
import com.nxw.springcloud.gateway.config.RateRedisRateLimiter;
import com.nxw.springcloud.gateway.entity.CommonConstants;
import com.nxw.springcloud.gateway.entity.EnvironmentEnum;
import com.nxw.springcloud.gateway.spi.GateWayAdminRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import static com.nxw.springcloud.gateway.entity.CommonConstants.TIME_TYPE;

/**
 * @program: gateway-service
 * @description:
 * @author: nxw
 * @create: 2020-02-29 14:27
 **/
@Service
public class RedisService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    @Lazy
    GateWayAdminRemote gateWayAdminRemote;

    @Autowired
    Environment environment;

    public RateRedisRateLimiter.Config getAppKetLimit(String appKeyAndSecret, RateRedisRateLimiter.Config appKeyConfig) {
        if (!StringUtils.isEmpty(appKeyAndSecret)) {
            String[] split = appKeyAndSecret.split("#");
            String appKey = split[0];
            String appSecret = split[1];
            String environment = split[2];
            String info = (String) redisTemplate.opsForHash().get(environment,appKey+"#"+appSecret);
            if (StringUtils.isEmpty(info)) {
                ChanjetValueResult<String> appKeyAndSecretRate = gateWayAdminRemote.getAppKeyAndSecret(appKey, EnvironmentEnum.getEnvironmentEnumByValue(environment));
                if (appKeyAndSecretRate == null){
                    if (appKeyConfig != null && appKeyConfig.getReplenishRate() > 0) {
                        return appKeyConfig;
                    }
                    RateRedisRateLimiter.Config config = new RateRedisRateLimiter.Config();
                    config.setReplenishRate(CommonConstants.DEFAULT_REPLENISH_RATE);
                    config.setTimeType(TIME_TYPE);
                    return config;
                } else {
                    return JSONObject.parseObject(appKeyAndSecretRate.getValue(), RateRedisRateLimiter.Config.class);
                }
            } else {
                return JSONObject.parseObject(info, RateRedisRateLimiter.Config.class);
            }
        } else {
            RateRedisRateLimiter.Config config = new RateRedisRateLimiter.Config();
            config.setReplenishRate(CommonConstants.DEFAULT_REPLENISH_RATE);
            config.setTimeType(TIME_TYPE);
            return config;
        }

    }

    public void refreshRateLimitConfig(String routeId) {
        String configStr = (String) redisTemplate.opsForValue().get(routeId);
        if (StringUtils.isEmpty(configStr)) {
            String forObject = gateWayAdminRemote.getRouteById(routeId);
            assert !StringUtils.isEmpty(forObject);
            RateLimitConfig rateLimitConfig = JSONObject.parseObject(forObject, RateLimitConfig.class);
            RateRedisRateLimiter.rateLimitConfigMap.put(routeId, rateLimitConfig);
        } else {
            RateLimitConfig rateLimitConfig = JSONObject.parseObject(configStr, RateLimitConfig.class);
            RateRedisRateLimiter.rateLimitConfigMap.put(routeId, rateLimitConfig);
        }

    }
}
